/**
 * Created by Administrator on 2016/6/14.
 */
var serviceHttp = 'http://inmycars.ihengtian.top/';
// var serviceHttp = 'http://192.168.1.240:8999/';
//  var serviceHttp = 'http://localhost:8999/';

var c_user = "";

/*index-路由*/
var indexapp = angular.module('indexRoute', ['ngRoute', 'ngAnimate']);

function routeConfig($routeProvider) {
    $routeProvider.when('/', {
        controller: 'mainController',
        templateUrl: 'lou_distribute.html'
    }).when('/lou/:louId', {
        controller: 'louController',
        templateUrl: 'lou_distribute.html'
    }).when('/ceng_distribute.html/:buildId/:louName', {
        controller: 'cengController',
        templateUrl: 'ceng_distribute.html'
    }).when('/shi_distribute.html/:floorId/:floorName', {
        controller: 'shiController',
        templateUrl: 'shi_distribute.html'
    }).when('/unit_distribute.html/:roomId', {
        controller: 'unitController',
        templateUrl: 'unit_distribute.html'
    }).when('/unit_Details.html/:unitId', {
        controller: 'unitDetailController',
        templateUrl: 'unit_Details.html'
    }).when('/set', {
        controller: 'settingController',
        templateUrl: 'Setting.html'
    }).when('/userList', {
        controller: 'userListController',
        templateUrl: 'userList.html'
    }).when('/userUnit', {
        controller: 'userUnitController',
        templateUrl: 'userUnit.html'
    }).when('/userDetails', {
        controller: 'userDetailsController',
        templateUrl: 'userDetails.html'
    }).when('/chartReport', {
        controller: 'chartReportController',
        templateUrl: 'chartReport.html'
    }).when('/platformUser', {
        controller: 'plateformUserController',
        templateUrl: 'plateformUser.html'
    }).when('/userList.html', {
        controller: 'plateformUserController',
        templateUrl: 'userList.html'
    }).when('/companyList', {
        controller: 'companyListController',
        templateUrl: 'companyList.html'
    }).otherwise({
        redirectTo: '/'
    });
};

indexapp.config(routeConfig);

/*首页分布*/

c_user = window.location.search.substr(1).split('&')[0].substr(5);
indexapp.controller('mainController', function ($http, $scope, $location) {
    //document.cookie = "JSESSIONID=6509BE029F71847F4322F7492C8D0E46";
    //$scope.c_user = c_user;
    $http.getData = function (url) {
        return $http.get(url).error(function () {
            window.location.href = "Lab_login.html";
        });
    }
    $scope.c_user = c_user;
    // if (c_user && c_user.length > 0) {
    //     $scope.c_user = c_user;
    // } else {
    //     alert("您还没有登录，请先登录！");
    //     window.location.href = "Lab_login.html";
    // }

    $scope.redirect = function (url) {
        $location.url(url);
    }

    $scope.menuList=[];

    $http.get(serviceHttp + 'api/rest/firm/person/mine/info?name=' + $scope.c_user).success(function (res) {
            $scope.currentUser = res.data;
            if($scope.currentUser.role.roleAuthority.id==3){
                    $scope.menuList = [
                        {name: '楼 分 布', url:'#/lou',menuId:'lou-Distribute'},
                        {name: '设    置', url:'#/set',menuId:'plate-set'},
                        {name: '用户列表', url:'#/userList',menuId:'user-List'},
                        {name: '用户设备', url:'#/userUnit',menuId:'user-Unit'},
                        {name: '统计报告', url:'#/chartReport',menuId:'chart-Report'}
                    ];
            }else if($scope.currentUser.role.roleAuthority.id==4){
                    $scope.menuList = [
                        {name: '楼 分 布', url:'#/lou',menuId:'lou-Distribute'},
                        {name: '用户设备', url:'#/userUnit',menuId:'user-Unit'},
                        {name: '统计报告', url:'#/chartReport',menuId:'chart-Report'}
                    ];
            }else {
    $scope.menuList = [
        {name: '用户列表', url:'#/userList',menuId:'user-List'},
        {name: '公司列表', url:'#/companyList',menuId:'company-List'}
    ];
            }

    });

//    $scope.menuList = [
//        {name: '楼 分 布', url:'#/lou',menuId:'lou-Distribute'},
//        {name: '设    置', url:'#/set',menuId:'plate-set'},
//        {name: '用户列表', url:'#/userList',menuId:'user-List'},
//        {name: '用户设备', url:'#/userUnit',menuId:'user-Unit'},
//        {name: '公司列表', url:'#/companyList',menuId:'company-List'},
//        {name: '统计报告', url:'#/chartReport',menuId:'chart-Report'}
//    ];

});

//悬浮内容展示
indexapp.directive('hover', [function () {
    return {
        restrict: 'A',
        link: function (scope, ele, attrs) {
            ele.bind('mouseover', function (e) {
                if ($(".previewContent:hidden").length > 0) {
                    $('.previewContent').hide();
                    $(this).parent('li').find(".previewContent").css('display', 'block').css('z-index', '99').fadeIn('slow');
                    scope.preViewPosition = {
                        left: (e.clientX + 15 + document.body.scrollLeft) + "px",
                        top: (e.clientY - 30 + document.body.scrollTop) + "px"
                    }
                    $(".previewContent").css('top', scope.preViewPosition.top);
                    $(".previewContent").css('left', scope.preViewPosition.left);
                }
            });
            ele.bind('mouseout', function (e) {
                $(".previewContent").fadeOut("fast");
            });
        }
    }
}]);

/*楼分布*/
var controlou = function ($http, $scope, $location, $routeParams) {

    function GetRequest() {
       var url = location.search; //获取url中"?"符后的字串
       var theRequest = new Object();
       if (url.indexOf("?") != -1) {
          var str = url.substr(1);
          strs = str.split("&");
          for(var i = 0; i < strs.length; i ++) {
             theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
          }
       }
       return theRequest;
    }
    var Req = new Object();
    Req = GetRequest();
    alert(Req("name"));
    $http.getData(serviceHttp + 'api/rest/firm/buildings/' + c_user).success(function (res) {
        $scope.louDistribut = res.data.background;
        $scope.louData = res.data;
        $scope.louList = res.data.list;
    });

    $scope.redirect = function (url) {
        $location.url(url);
    }
}
indexapp.controller('louController', controlou);

/*层分布*/
var controceng = function ($http, $scope, $location, $routeParams) {

    $scope.buildId = $routeParams["buildId"];
    $scope.louName = $routeParams["louName"];
    $http.get(serviceHttp + 'api/rest/firm/floors?buildId=' + $scope.buildId).success(function (res) {
        $scope.CengList = res.data.floors;
        $scope.CengData = res.data;
        // console.log($scope.CengList);
        console.log($scope.CengData);
    });

    $scope.redirect = function (url) {
        $location.url(url);
    }
}
indexapp.controller('cengController', controceng);

/*室分布*/
var controshi = function ($http, $scope, $location, $routeParams) {
    $scope.buildId = $routeParams["buildId"];
    // $scope.louName = $routeParams["louName"];
    $scope.floorName = $routeParams["floorName"];
    $scope.floorId = $routeParams["floorId"];

    $http.getData(serviceHttp + 'api/rest/firm/rooms?buildId=' + $scope.buildId + '&floorId=' + $scope.floorId).success(function (res) {
        $scope.roomList = res.data.roomList;
        $scope.roomData = res.data;
        console.log($scope.roomList);
    });

    $scope.redirect = function (url) {
        $location.url(url);
    }
}
indexapp.controller('shiController', controshi);

/*设备分布*/
var controunit = function ($http, $scope, $location, $routeParams) {
    $scope.buildId = $routeParams["buildId"];
    $scope.floorId = $routeParams["floorId"];
    $scope.roomId = $routeParams["roomId"];
    $http.getData(serviceHttp + 'api/rest/firm/devices?roomId=' + $scope.roomId).success(function (res) {
        $scope.unitData = res.data;
        $scope.unitList = res.data.deviceList;
    });

    $http.getData(serviceHttp + 'api/rest/room/current/data?roomId=' + $scope.roomId).success(function (res) {
        $scope.houseValue = res.data;
        console.log($scope.houseValue);
    });

    $scope.redirect = function (url) {
        $location.url(url);
    }
}
indexapp.controller('unitController', controunit);

/*设备详情*/
var controunitDetails = function ($http, $scope, $location, $routeParams, $q, $interval) {

    $scope.unitId = $routeParams["unitId"];
    $scope.unitDeaTData = {};
    $scope.deviceId = '';


    //修改设备详情--设备背景图
    $scope.updateUnitPhotoInfo = function () {
    var path = $("#unitfileToUpload").val();
    if(path){
        $.ajaxFileUpload({
            url:serviceHttp +'api/rest/file/change/picture/'+$scope.unitId,
            secureuri: false,
            fileElementId: 'unitfileToUpload',//file控件id
            success: function (res) {

            }
        });
    }
    }

    $scope.getFloorList = function () {
        $http.getData(serviceHttp + 'api/rest/firm/device?deviceId=' + $scope.unitId).success(function (res) {
            $scope.unitDeaTData = res.data;
            $scope.deviceId = res.data.id;
            $scope.unitdataFloors = res.data.deviceFloors;
            // console.log($scope.unitDeaTData);
        });
    }

    //获取设备管理员
    $scope.getManager = function () {
        $http.getData(serviceHttp + 'api/rest/firm/colleges/' + c_user).success(function (res) {
            $scope.selected = "";
            $scope.manager = res.data;
             console.log($scope.manager);
        });

    }
    $scope.getManager();

    //修改设备基本概况--保存
    $scope.updateUnitDetail = function () {
//        var deviceId = $scope.unitDeaTData.id;
//        var name = $scope.unitDeaTData.name;
//        var creator = $scope.unitDeaTData.creator;
//        var model = $scope.unitDeaTData.model;
//        var purchase = $scope.unitDeaTData.purchase;
//        var maintainDate = $scope.unitDeaTData.maintainDate;
//        var maintain = $scope.unitDeaTData.maintain;
//        var selected = $scope.selected.id;
        $http.getData(serviceHttp + 'api/rest/operate/device/' + $scope.unitDeaTData.id + '?name=' +
         $scope.unitDeaTData.name + '&creator=' + $scope.unitDeaTData.creator + '&maintain=' + $scope.unitDeaTData.maintain +
         '&maintainDate=' + $scope.unitDeaTData.maintainDate + '&model=' + $scope.unitDeaTData.model +
         '&purchase=' + $scope.unitDeaTData.purchase + '&managerId=' + $scope.selected.id).success(function (res) {
//            $scope.getFloorList();
        });
    }

    $scope.getFloorList();

    $http.getData(serviceHttp + 'api/rest/device/current/data?deviceId=' + $scope.unitId).success(function (res) {
        $scope.inspectData = res.data;
        $scope.inspects = res.data.list;
        console.log($scope.inspects);
        $scope.yiji_paramsBgColor = {"background-color": "#FFCC33"};
        $scope.erji_paramsBgColor = {"background-color": "#FFCC99"};
        $scope.biaozhun_paramsBgColor = {"background-color": "#00CC66"};

        clearTimeout(renderTimer);
        var renderTimer = setTimeout(renderChart, 1000);

        function renderChart() {
            var data = $scope.inspects;
            // console.log(data[0][0].deviceInspect.start);
            $('.fl.ml10 div').each(function () {
                var id = $(this).attr('id');
                if (id) {
                    var flow = [];
                    var scale = {};
                    var index = id.substr(9);
                    if (data[index]) {
                        scale = {
                            start_scale: parseInt(data[index][0].deviceInspect.start),
                            end_scale: parseInt(data[index][0].deviceInspect.end),
                            scale_space: parseInt(data[index][0].deviceInspect.value)
                        };

                        // scale.start_scale = Math.ceil(data[index][0].deviceInspect.start);
                        // scale.end_scale = Math.ceil(data[index][0].deviceInspect.end);
                        // scale.scale_space = Math.ceil(data[index][0].deviceInspect.value);

                        data[index].each(function (item, index) {
                            flow.push(item.picResult);
                        })
//                        creatIChart(id, flow, scale);
                    }
                    // console.log(scale);
                }
            })

            clearTimeout(renderTimer);
            renderTimer = setTimeout(renderChart, 200);
        }

    });

    var timer = $interval(function () {
        $http.getData(serviceHttp + 'api/rest/device/current/data?deviceId=' + $scope.unitId).success(function (res) {
            $scope.inspects = res.data.list;
        });
    }, 10000);

    //修改设备警报参数初始化
    // $scope.updateAlertCSInit = function () {
    //
    // }

    $scope.devicePara = {};
    //修改设备警报参数
    $scope.updateAlertCSInfo = function () {
        $http.post(serviceHttp + 'api/rest/operate/device/parameter/'+$scope.unitDeaTData.id,$scope.devicePara).success(function(res){
            $scope.unitDeaTData = res.data;
        });
    }

    $scope.getDeviceAlterParameter = function(){

        $http.getData(serviceHttp + 'api/rest/operate/get/device/parameter/'+ $scope.unitDeaTData.id).success(function(res){
            $scope.devicePara = res.data;
        });
    }

    $scope.redirect = function (url) {
        $location.url(url);
    }

    /*新增层信息*/
    $scope.addfloorInfo = function () {

        $("#floorNum").val(""); //第几层
        $("#scientist").val(""); //
        $("#unitType").val(""); //样品名称
        $("#productNum").val(""); //样品数量
        $("#email").val(""); //
        $("#mobile").val(""); //

        var type = 0;
        var floorNum = $scope.floorNum;
        var scientist = $scope.scientist;
        var unitName = $scope.unitType;
        var productNum = $scope.productNum;//样品数量
        var email = $scope.email;
        var mobile = $scope.mobile;
        $http.getData(serviceHttp + 'api/rest/operate/device/floor/' + $scope.deviceId + '?type=' + type + '&floorNum=' + floorNum + '&scientist=' + scientist + '&name=' + unitName + '&productNum=' + productNum + '&email=' + email + '&mobile=' + mobile).success(function (res) {
            $scope.getFloorList();
        });
    }

    //选中层修改
    $scope.selectedUnitFloorInfo = function (currentUnitFloor) {
        $scope.selectCurrUnitFloor = currentUnitFloor || this.unitfloor;
        $scope.CurrentFloorId = $scope.selectCurrUnitFloor.id;
        console.log($scope.selectCurrUnitFloor);
        $scope.floorNum = $scope.selectCurrUnitFloor.floorNum;
        $scope.unitType = $scope.selectCurrUnitFloor.name;
        $scope.productNum = $scope.selectCurrUnitFloor.productNum;
        $scope.scientist = $scope.selectCurrUnitFloor.scientist;
        $scope.mobile = $scope.selectCurrUnitFloor.mobile;
        $scope.email = $scope.selectCurrUnitFloor.email;
    }

    /*修改层信息*/
    $scope.updatefloorInfo = function () {
        var floorId = $scope.CurrentFloorId;
        var type = 1;
        var floorNum = $scope.floorNum;
        var scientist = $scope.scientist;
        var unitName = $scope.unitType;
        var productNum = $scope.productNum;//样品数量
        var email = $scope.email;
        var mobile = $scope.mobile;
        $http.getData(serviceHttp + 'api/rest/operate/device/floor/' + $scope.deviceId + '?type=' + type + '&floorId=' + floorId + '&floorNum=' + floorNum + '&scientist=' + scientist + '&name=' + unitName + '&productNum=' + productNum + '&email=' + email + '&mobile=' + mobile).success(function (res) {
            $scope.getFloorList();
        });
    }
}

indexapp.controller('unitDetailController', controunitDetails);

/*用户列表*/
var controUserList = function ($http, $scope, $location) {

    $scope.total = 0;     // 总记录数
    $scope.pageTotal = 0; // 总页数
    $scope.pageIndex = 0;	 //当前页
    $scope.range = function (start, end) {
        var ret = [];
        if (!end) {
            end = start;
            start = 0;
        }
        for (var i = start; i < end; i++) {
            ret.push(i);
        }
        return ret;
    };

    $scope.prevPage = function () {
        if ($scope.pageIndex > 0) {
            $scope.pageIndex--;
        }
        $scope.getUserList();
    };

    $scope.nextPage = function () {
        if ($scope.pageIndex < $scope.pageTotal - 1) {
            $scope.pageIndex++;
        }
        $scope.getUserList();
    };

    $scope.setPage = function () {
        $scope.pageIndex = this.n;
        $scope.getUserList();
    };

    $scope.limit = 10;
    $scope.userName = "";
    $scope.mobile = "";
    $scope.jobNumber = "";

    $scope.getUserList = function () {
        var start = $scope.pageIndex * $scope.limit;
        var limit = $scope.limit;
        var jobNumber = $scope.jobNumber;
        var userName = $scope.userName;
        var mobile = $scope.mobile;
        $http.getData(serviceHttp + 'api/rest/firm/employees/' + c_user + '?start=' + start + '&limit=' + limit + '&userName=' + userName + '&mobile=' + mobile).success(function (res) {
            $scope.total = res.data.total;
            if ($scope.limit > 0) {
                $scope.pageTotal = parseInt($scope.total / $scope.limit) + ($scope.total % $scope.limit > 0 ? 1 : 0);
            }
            $scope.getUser = res.data.userList;
            console.log($scope.getUser);
        });
    }
    $scope.getUserList();

    $scope.getUserCompany = function(){
        $http.getData(serviceHttp + 'api/rest/firm/query/all/company/'+c_user).success(function(res){
            $scope.myCompanyList = res.data;
        });
    };

    $scope.createUserByMe = function(){
    var newAccount = $scope.newAccount;
    var companyId = $scope.selected.id;
    var userName = $scope.newUserName;
    var department = $scope.newDepartment;
    var jobNum = $scope.newJobNum;
    var job = $scope.newJob;

    $http.getData(serviceHttp + 'api/rest/operate/create/user/'+c_user+'?name='+newAccount+'&companyId='+companyId
        +'&userName='+userName+'&department='+department+'&jobNum='+jobNum+'&job='+job).success(function(res){
            $scope.getUserList();
        });
    }

    $scope.redirect = function (url) {
        $location.url(url);
    }

}

/*公司列表*/
var controCompanyList = function ($http, $scope, $location) {
    $scope.currentCompany = {};
    $http.getData(serviceHttp + 'api/rest/firm/query/all/company/' + c_user).success(function (res) {
                $scope.allCompany = res.data;
    });

    $scope.addCompanyDialog = function(){
        $scope.currentCompany = {};
    }

    $scope.addCompany = function(){
        var path = $("#addCompanyToUpload").val();

            $.ajaxFileUpload({
                url:serviceHttp + 'api/rest/file/create/company/' + c_user+'?name='+$scope.currentCompany.name
                                 +'&address='+$scope.currentCompany.address+'&email='+$scope.currentCompany.email+'&telephone='+$scope.currentCompany.telephone,
                secureuri: false,
                fileElementId: 'addCompanyToUpload',//file控件id
                success: function (res) {
                }
            });

    }

    $scope.updateCompany = function(){
    var path = $("#updateCompanyToUpload").val();

            $.ajaxFileUpload({
                url:serviceHttp + 'api/rest/file/create/company/' + c_user+'?id='+$scope.currentCompany.id+'&name='+$scope.currentCompany.name
                                     +'&address='+$scope.currentCompany.address+'&email='+$scope.currentCompany.email+'&telephone='+$scope.currentCompany.telephone,
                secureuri: false,
                fileElementId: 'updateCompanyToUpload',//file控件id
                success: function (res) {
                }
            });

    }

    $scope.UpdateCurrentCompany = function(company){
        $scope.currentCompany = company;
    }

    $scope.redirect = function (url) {
        $location.url(url);
    }

}

indexapp.controller('companyListController',controCompanyList);
indexapp.controller('userListController', controUserList);

indexapp.controller('settingController', controSetting);
/*设置*/
var controSetting = function ($http, $scope, $location) {

    $scope.showType = 1;
    $scope.buildId = "";
    $scope.floorId = "";
    $scope.roomId = "";
    $scope.setDate = [];
    $scope.preview = function (file, imgId) {
        var img = $('#' + imgId),
            url = URL.createObjectURL(file);
        img.unbind();
        img.bind('load', function () {
            URL.revokeObjectURL(url);
        })
        img.attr('src', url);
    }
    $scope.redirect = function (url) {
        $location.url(url);
    }

    $scope.showUnitType = function () {
        $scope.showType = 1;
        $scope.MyColor1 = {"background-color": "#08c"}
        $scope.MyColor2 = {"background-color": "#00CC66"}
    }
    $scope.showFloorManage = function () {
        $scope.showType = 2;
        $scope.MyColor1 = {"background-color": "#08c"}
        $scope.MyColor2 = {"background-color": "#00CC66"}
    }

    //设备列表
    $http.getData(serviceHttp + 'api/rest/firm/device/types').success(function (res) {
        $scope.setDate = res.data;
        // console.log($scope.setDate);
    });

    //设置--新增设备弹出框初始化
    $scope.AddUnitTypeInit = function () {

        $http.getData(serviceHttp + 'api/rest/firm/query/inspect/type').success(function (res) {
            $scope.inspectUnitType = res.data;
            console.log($scope.inspectUnitType);
        });

        $("#addUnitTypeToUpload").unbind('change').change(function (e) {
            var file = e.target.files[0];
            $scope.preview(file, 'addUnitTypePhotoNew');
        });
        $("#updateUnitTypeToUpload").unbind('change').change(function (e) {
            var file = e.target.files[0];
            $scope.preview(file, 'updateUnitTypePhotoNew');
        });

        $scope.selected = [];
        $scope.selectedTags = [];

        // action: 1--选中；0--未选中
        var updateSelected = function (action, id, name) {
            if (action == '1' && $scope.selected.indexOf(id) == -1) {

                $scope.selectCK = {
                    id: id,
                    name: name
                };

                $scope.selected.push(id);
                $scope.selectedTags.push(name);
                // $(".R_blockStyle input[type='text']").attr('readonly',false);
                // $(this).parent('li').find(".R_blockStyle input[type='text']").attr('readonly',false);
            }
            if (action == '0' && $scope.selected.indexOf(id) != -1) {
                var idx = $scope.selected.indexOf(id);
                $scope.selected.splice(idx, 1);
                $scope.selectedTags.splice(idx, 1);
            }
        }

        $scope.selectedCheck = function ($event, id) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? '1' : '0');
            updateSelected(action, id, checkbox.name);
        }

        //$scope.selected.indexOf(id)-- -1:未选中; 0:选中
        $scope.isSelected = function (id) {
            return $scope.selected.indexOf(id) >= 0;
          //  alert($scope.selected.indexOf(id));
        }


    }

    //保存新增设备
    $scope.SaveAddUnitTypeInfo = function () {

    var path = $("#addUnitTypeToUpload").val();

    $http.post(serviceHttp + 'api/rest/operate/deviceType/' + c_user,$scope.inspectUnitType).success(function(res){
            if(path){
                $.ajaxFileUpload({
                                url: serviceHttp + 'api/rest/file/upload/deviceType/icon/' + res.data.id,
                                secureuri: false,
                                fileElementId: 'addUnitTypeToUpload',//file控件id
                                success: function (data, status) {
                                        $http.getData(serviceHttp + 'api/rest/firm/device/types').success(function (res) {
                                                        $scope.setDate = res.data;
                                                        // console.log($scope.setDate);
                                                });
                                    }
                                });
            }

        }).error(function(data){
            console.log("错了！");
        });
    }

    //修改设备弹出框初始化
    $scope.UpdateUnitTypeInit = function (id) {
        $http.getData(serviceHttp + 'api/rest/firm/device/type/request/'+id).success(function (res) {
            $scope.currentDeviceTypeReq = res.data;
        }).error(function(){
            alert("error")
        });
    }
    //保存修改设备
    $scope.SaveUpdateUnitTypeInfo = function () {
        $http.post(serviceHttp + 'api/rest/operate/deviceType/' + c_user,$scope.currentDeviceTypeReq).success(function(res){
        var path = $("#updateUnitTypeToUpload").val();
            if(path){
                                        $.ajaxFileUpload({
                                        url: serviceHttp + 'api/rest/file/upload/deviceType/icon/' + res.data.id,
                                        secureuri: false,
                                        fileElementId: 'updateUnitTypeToUpload',//file控件id
                                        success: function (data, status) {
                                                $http.getData(serviceHttp + 'api/rest/firm/device/types').success(function (res) {
                                                                $scope.setDate = res.data;
                                                                // console.log($scope.setDate);
                                                        });
                                            }
                                        });
            }
        }).error(function(data){
            console.log("错了！");
        });
    }

    /*楼层管理*/

    //获取楼列表
    $scope.getlouList = function () {
        $http.getData(serviceHttp + 'api/rest/firm/buildings/' + c_user).success(function (res) {
            $scope.louData = res.data;
            // console.log(res.data);
            if (res.data && res.data.list.length > 0) {
                $scope.selectLou = $scope.louData.list[0];
            } else {
                $scope.selectLou = null;
            }
            $scope.selectedCurrentlou($scope.selectLou);

            //新增--楼--背景分布图
            $("#loufileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'addlouphotoNew');
            });
            //修改--楼--背景分布图
            $("#updateloufileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'updatelouphotoNew');
            });

        });
    }

    //选中当前楼
    $scope.selectedCurrentlou = function (currLou) {
        $scope.selectLou = currLou || this.lou;
        $scope.buildId = !$scope.selectLou ? "" : $scope.selectLou.id;
        $scope.CengList = null;
        $scope.getcengList();
        $scope.getshiList();
        $scope.getUnitList();
    }

    //新增楼弹出框初始化
    $scope.AddlouInit = function () {
        $scope.lou_name = "";
        $scope.lou_xpoint = "";
        $scope.lou_ypoint = "";
        $("#loufileToUpload").val('');
        $("#addlouphotoNew").attr('src', '');
    }

    //保存--新增楼--的值
    $scope.SaveAddlouInfo = function () {

        var path = $("#loufileToUpload").val();
        var type = 0;// 0:新增；1:修改
        var lou_name = $scope.lou_name;
        var lou_xpoint = $scope.lou_xpoint;
        var lou_ypoint = $scope.lou_ypoint;
        if (path) {
            $.ajaxFileUpload({
                url: serviceHttp + 'api/rest/file/create/building/' + c_user + '?name=' + lou_name + '&xpoint=' + lou_xpoint + '&ypoint=' + lou_ypoint + '&type=' + type,
                secureuri: false,
                fileElementId: 'loufileToUpload',//file控件id
                dataType: 'json',
                success: function (data, status) {
                    $scope.getlouList();
                },
                error: function (data, status, e) {
                    // alert(e);
                    console.log(e);
                }
            });
        } else {
            $scope.getlouList();
        }
    }

    //修改当前楼弹出框初始化
    $scope.UpdatelouInit = function (currentLou) {

        var selectCurrentLou = currentLou || this.lou;
        console.log(selectCurrentLou);
        $scope.lou_name = selectCurrentLou.name;
        $scope.lou_xpoint = selectCurrentLou.xpoint;
        $scope.lou_ypoint = selectCurrentLou.ypoint;

    }

    //保存--修改楼--的值
    $scope.SaveUpdatelouInfo = function () {
        if (!this.selectLou) {
            return;
        }
        var buildId = this.selectLou.id;
        //alert(buildId);
        var type = 1;// 0:新增；1:修改
        var lou_name = $scope.lou_name;
        var lou_xpoint = $scope.lou_xpoint;
        var lou_ypoint = $scope.lou_ypoint;
        $.ajaxFileUpload({
            url: serviceHttp + 'api/rest/file/create/building/' + c_user + '?name=' + lou_name + '&xpoint=' + lou_xpoint + '&ypoint=' + lou_ypoint + '&type=' + type + '&buildId=' + buildId,
            secureuri: false,
            fileElementId: 'updateloufileToUpload',//file控件id
            dataType: 'json',
            success: function (data, status) {
                $scope.getlouList();
            },
            error: function (data, status, e) {
                // alert(e);
                console.log(e);
            }
        });
    }

    //获取层列表
    $scope.getcengList = function () {
        if (!$scope.selectLou) {
            $scope.CengList = [];
            return;
        }
        var buildId = $scope.selectLou.id;
        $http.getData(serviceHttp + 'api/rest/firm/floors?buildId=' + buildId).success(function (res) {
            $scope.cengData = res.data;
            // console.log(res.data.floors);
            console.log(res.data);
            if (res.data && res.data.floors.length > 0) {
                $scope.selectCeng = res.data.floors[0];
            } else {
                $scope.selectCeng = null;
            }
            $scope.selectedCurrentCeng($scope.selectCeng);

            $("#cengfileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'addcengphotoNew');
            });

            $("#updateCengfileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'updateCengphotoNew');
            });
        });
    }

    //选中当前层
    $scope.selectedCurrentCeng = function (currCeng) {
        $scope.selectCeng = currCeng || this.ceng;
        $scope.floorId = !$scope.selectCeng ? "" : $scope.selectCeng.id;
        $scope.roomList = null;
        $scope.getshiList();
        $scope.getUnitList();
    }

    //新增层弹出框初始化
    $scope.AddCengInit = function () {
        $scope.ceng_name = "";
        $scope.ceng_xpoint = "";
        $scope.ceng_ypoint = "";
        $("#cengfileToUpload").val('');
        $("#addcengphotoNew").attr('src', '');
    }

    //保存--新增层--的值
    $scope.SaveAddCengInfo = function () {
        if (!$scope.selectLou) {
            return;
        }
        var path = $("#cengfileToUpload").val();
        var type = 0;// 0:新增；1:修改
        var buildId = this.selectLou.id;
        var ceng_name = $scope.ceng_name;
        var ceng_xpoint = $scope.ceng_xpoint;
        var ceng_ypoint = $scope.ceng_ypoint;
        if (path) {
            $.ajaxFileUpload({
                url: serviceHttp + 'api/rest/file/create/floor/' + c_user + '?name=' + ceng_name + '&xpoint=' + ceng_xpoint + '&ypoint=' + ceng_ypoint + '&type=' + type + '&buildId=' + $scope.buildId,
                secureuri: false,
                fileElementId: 'cengfileToUpload',//file控件id
                dataType: 'json',
                success: function (data, status) {
                    $scope.getlouList();
                },
                error: function (data, status, e) {
                    console.log(e);
                }
            });
        } else {
            $scope.getlouList();
        }
    }

    //修改当前层初始化弹出框
    $scope.UpdateCengInit = function (currentCeng) {
        var selectCurrentCeng = currentCeng || this.ceng;
        console.log(selectCurrentCeng);
        $scope.ceng_name = selectCurrentCeng.name;
        $scope.ceng_xpoint = selectCurrentCeng.xpoint;
        $scope.ceng_ypoint = selectCurrentCeng.ypoint;
    }

    //保存--修改层--的值
    $scope.SaveUpdateCengInfo = function () {
        if (!this.selectLou || !this.selectCeng) {
            return;
        }
        var buildId = this.selectLou.id;
        var floorId = this.selectCeng.id;
        var type = 1;// 0:新增；1:修改
        var ceng_name = $scope.ceng_name;
        var ceng_xpoint = $scope.ceng_xpoint;
        var ceng_ypoint = $scope.ceng_ypoint;
        $.ajaxFileUpload({
            url: serviceHttp + 'api/rest/file/create/floor/' + c_user + '?name=' + ceng_name + '&xpoint=' + ceng_xpoint + '&ypoint=' + ceng_ypoint + '&type=' + type + '&buildId=' + buildId + '&floorId=' + floorId,
            secureuri: false,
            fileElementId: 'updateCengfileToUpload',//file控件id
            dataType: 'json',
            success: function (data, status) {
                $scope.getlouList();
            },
            error: function (data, status, e) {
                // alert(e);
                console.log(e);
            }
        });
    }

    //获取室列表
    $scope.getshiList = function () {
        if (!$scope.selectCeng) {
            $scope.roomList = [];
            return;
        }
        var buildId = $scope.buildId;
        var floorId = $scope.selectCeng.id;
        $http.getData(serviceHttp + 'api/rest/firm/rooms?buildId=' + buildId + '&floorId=' + floorId).success(function (res) {
            $scope.roomData = res.data;
            $scope.roomList = res.data.roomList;
            //console.log(res.data);
            if (res.data && res.data.roomList.length > 0) {
                $scope.selectShi = $scope.roomList[0];
            } else {
                $scope.selectShi = null;
            }
            $scope.selectedCurrentShi($scope.selectShi);

            $("#shifileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'addshiphotoNew');
            });

            $("#updateShifileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'updateShiphotoNew');
            });
        });
    }

    //选中当前室
    $scope.selectedCurrentShi = function (currShi) {
        $scope.selectShi = currShi || this.shi;
        $scope.roomId = !$scope.selectShi ? "" : $scope.selectShi.id;
        $scope.deviceList = null;
        $scope.getUnitList();
    }

    //新增室弹出框初始化
    $scope.AddshiInit = function () {
        $scope.shi_name = "";
        $scope.shi_xpoint = "";
        $scope.shi_ypoint = "";
        $("#shifileToUpload").val('');
        $("#addshiphotoNew").attr('src', '');
    }

    //保存--新增室--的值
    $scope.SaveAddShiInfo = function () {

        if (!$scope.selectCeng) {
            return;
        }
        var path = $("#shifileToUpload").val();
        var type = 0;// 0:新增；1:修改
        var floorId = this.selectCeng.id;
        var shi_name = $scope.shi_name;
        var shi_xpoint = $scope.shi_xpoint;
        var shi_ypoint = $scope.shi_ypoint;
        if (path) {
            $.ajaxFileUpload({
                url: serviceHttp + 'api/rest/file/create/room/' + c_user + '?name=' + shi_name + '&xpoint=' + shi_xpoint + '&ypoint=' + shi_ypoint + '&type=' + type + '&floorId=' + floorId,
                secureuri: false,
                fileElementId: 'shifileToUpload',//file控件id
                dataType: 'json',
                success: function (data, status) {
                    $scope.getlouList();
                },
                error: function (data, status, e) {
                    console.log(e);
                }
            });
        } else {
            $scope.getlouList();
        }
    }

    //修改室初始化弹出框
    $scope.UpdateShiInit = function (currentShi) {
        var selectCurrentShi = currentShi || this.shi;
        console.log(selectCurrentShi);
        $scope.shi_name = selectCurrentShi.name;
        $scope.shi_xpoint = selectCurrentShi.xPoint;
        $scope.shi_ypoint = selectCurrentShi.yPoint;
    }

    //保存--修改室--的值
    $scope.SaveUpdateShiInfo = function () {
        if (!$scope.selectCeng || !$scope.selectShi) {
            return;
        }
        var path = $("#shifileToUpload").val();
        var type = 1;// 0:新增；1:修改
        var floorId = this.selectCeng.id;
        var roomId = this.selectShi.id;
        var shi_name = $scope.shi_name;
        var shi_xpoint = $scope.shi_xpoint;
        var shi_ypoint = $scope.shi_ypoint;
        // if (path) {
        $.ajaxFileUpload({
            url: serviceHttp + 'api/rest/file/create/room/' + c_user + '?name=' + shi_name + '&xpoint=' + shi_xpoint + '&ypoint=' + shi_ypoint + '&type=' + type + '&floorId=' + floorId + '&roomId=' + roomId,
            secureuri: false,
            fileElementId: 'shifileToUpload',//file控件id
            dataType: 'json',
            success: function (data, status) {
                $scope.getlouList();
            },
            error: function (data, status, e) {
                console.log(e);
            }
        });
        //  } else {
        //      $scope.getlouList();
        //  }
    }

    //获取设备列表
    $scope.getUnitList = function () {
        if (!$scope.selectShi) {
            $scope.deviceList = [];
            return;
        }
        var roomId = $scope.selectShi.id;
        $http.getData(serviceHttp + 'api/rest/firm/devices?roomId=' + roomId).success(function (res) {
            // $scope.unitData = res.data;
            $scope.deviceList = res.data.deviceList;
            console.log(res.deviceList);
            if (res.data && res.data.deviceList.length > 0) {
                $scope.selectUnit = res.data.deviceList[0];
            } else {
                $scope.selectUnit = null;
            }
            $scope.selectedCurrentUnit($scope.selectUnit);

            $("#unitfileToUpload").unbind('change').change(function (e) {
                var file = e.target.files[0];
                $scope.preview(file, 'addunitphotoNew');
            });

        });
    }

    //选中当前设备
    $scope.selectedCurrentUnit = function (currUnit) {
        $scope.selectUnit = currUnit || this.unit;
        $scope.unitId = !$scope.selectUnit ? "" : $scope.selectUnit.id;
    }

    //新增设备弹出框初始化
    $scope.AddUnitInit = function () {
        $scope.unit_name = "";
        $scope.unit_code = "";
        $scope.unit_xpoint = "";
        $scope.unit_ypoint = "";
        $scope.unit_monitorCode = "";
        $("#unitfileToUpload").val('');
        $("#addunitphotoNew").attr('src', '');

        //设备列表
        $http.getData(serviceHttp + 'api/rest/firm/device/types').success(function (res) {
            $scope.selected = " ";
            $scope.selectUnit = res.data;
            console.log($scope.selectUnit);
        });
    }

    //获取设备管理员
    $scope.getManager = function () {
        $http.getData(serviceHttp + 'api/rest/firm/colleges/' + c_user).success(function (res) {
            $scope.manager = res.data;
            // console.log($scope.manager);
        });

    }
    $scope.getManager();

    //保存--新增设备
    $scope.SaveAddUnitInfo = function () {
        if (!$scope.selectShi) {
            return;
        }
        var path = $("#unitfileToUpload").val();
        var typeId = $scope.selected.id;//所选设备种类Id
        var roomId = this.selectShi.id;
        var unit_code = $scope.unit_code;
        var unit_name = $scope.unit_name;
        var unit_xpoint = $scope.unit_xpoint;
        var unit_ypoint = $scope.unit_ypoint;
        var unit_managerId = $scope.selectManager.id;  //设备管理员
        var unit_monitorCode = $scope.unit_monitorCode; //终端编号
        if (path) {
            $.ajaxFileUpload({
                url: serviceHttp + "api/rest/file/create/device/" + c_user + "?roomId=" + roomId + "&typeId=" + typeId + "&code=" + unit_code + "&managerId=" + unit_managerId + "&xPoint=" + unit_xpoint + "&yPoint=" + unit_ypoint + "&name=" + unit_name + "&monitorCode=" + unit_monitorCode,
                secureuri: false,
                fileElementId: 'unitfileToUpload',//file控件id
                dataType: 'json',
                success: function (data, status) {
                    $scope.getUnitList();
                },
                error: function (data, status, e) {
                    console.log(e);
                }
            });
        } else {
            $scope.getUnitList();
        }
    }
    $scope.getlouList();
}
indexapp.controller('settingController', controSetting);

/*用户设备*/
var controuserUnit = function ($http, $scope, $location) {

    $scope.total = 0;     // 总记录数
    $scope.pageTotal = 0; // 总页数
    $scope.pageIndex = 0;	 //当前页
    $scope.range = function (start, end) {
        var ret = [];
        if (!end) {
            end = start;
            start = 0;
        }
        for (var i = start; i < end; i++) {
            ret.push(i);
        }
        return ret;
    };

    $scope.prevPage = function () {
        if ($scope.pageIndex > 0) {
            $scope.pageIndex--;
        }
        $scope.getUserUnitList();
    };

    $scope.nextPage = function () {
        if ($scope.pageIndex < $scope.pageTotal - 1) {
            $scope.pageIndex++;
        }
        $scope.getUserUnitList();
    };

    $scope.setPage = function () {
        $scope.pageIndex = this.n;
        $scope.getUserUnitList();
    };

    $scope.limit = 10;
    $scope.code = "";
    $scope.mobile = "";
    $scope.name = "";
    $scope.code = "";
    $scope.typeId = "";
    //$scope.userId = "";
    $scope.getUserUnitList = function () {

        var start = $scope.pageIndex * $scope.limit;
        var limit = $scope.limit;
        var code = $scope.code;
        var mobile = $scope.mobile;
        var name = $scope.name;
        var typeId = $scope.typeId;
        var userId = 1;
        $http.getData(serviceHttp + ' api/rest/firm/manager/devices/' + c_user + '?start=' + start + '&limit=' + limit + '&code=' + code + '&name=' + name + '&typeId=' + typeId + '&userId=' + userId).success(function (res) {
            $scope.total = res.data.total;
            if ($scope.limit > 0) {
                $scope.pageTotal = parseInt($scope.total / $scope.limit) + ($scope.total % $scope.limit > 0 ? 1 : 0);
            }
            // $scope.getUserUnit = res.data;
            $scope.getUserDevice = res.data.devices;
            console.log($scope.getUserDevice);
        });
    }

    $scope.redirect = function (url) {
        $location.url(url);
    }

    $scope.getUserUnitList();

}
indexapp.controller('userUnitController', controuserUnit);

/*平台业务员*/
var controPlateformUser = function ($http, $scope, $location) {

    $scope.redirect = function (url) {
        $location.url(url);
    }
}
indexapp.controller('plateformUserController', controPlateformUser);
